package com.ckd.flyingtrip.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.javabean.HomePopularBean;
import com.ckd.flyingtrip.utils.StringUtils;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyHolder> {

    private Context context;
    private List<HomePopularBean.DataBean.ListBean> list;
    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public HomeAdapter(Context context, List<HomePopularBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_home_recyclerview, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        Glide.with(context).load(list.get(position).getBicyclePictureUrl()).into(holder.imageView);
        holder.name.setText(list.get(position).getBicycleName());
        
        holder.money.setText("¥" + StringUtils.GetZero(list.get(position).getBicycleRent()) + "/" + list.get(position).getBicycleUseDays() + "天");

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView name, money;
        LinearLayout linearLayout;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.item_home_recyclerview_ll);
            imageView = itemView.findViewById(R.id.item_home_recyclerview_icon);
            name = itemView.findViewById(R.id.item_home_recyclerview_name);
            money = itemView.findViewById(R.id.item_home_recyclerview_money);
        }
    }
}
